package group.igfd.manager.biz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import group.igfd.manager.biz.dao.TagHotDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import group.igfd.manager.biz.entity.TagHotEntity;
import group.igfd.manager.biz.service.TagHotService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 选购热点
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-11-01 16:23:18
 */
@RestController
@RequestMapping("biz/tagHot")
@Api(tags = {"A006/ 标签管理"})
public class TagHotController {
    @Autowired
    private TagHotService tagHotService;

    @Autowired
    private TagHotDao tagHotDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:tagHot:list")
    @ApiOperation("获取热门标签信息")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tagHotService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:tagHot:info")
    public R info(@PathVariable("id") Integer id) {
        TagHotEntity tagHot = tagHotService.selectById(id);

        return R.ok().put("tagHot", tagHot);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:tagHot:save")
    public R save(@RequestBody TagHotEntity tagHot) {
        int i = tagHotDao.checkTagHot(null, tagHot.getName());
        if (i > 0) {
            return R.error("标签已存在");
        }
        Date now = new Date();
        tagHot.setCreatedAt(now);
        tagHot.setUpdatedAt(now);
        tagHotService.insert(tagHot);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:tagHot:update")
    public R update(@RequestBody TagHotEntity tagHot) {
        int i = tagHotDao.checkTagHot(tagHot.getId(), tagHot.getName());
        if (i > 0) {
            return R.error("标签已存在");
        }
        tagHot.setUpdatedAt(new Date());
        tagHotService.updateById(tagHot);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:tagHot:delete")
    public R delete(@RequestBody Integer[] ids) {
        tagHotService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
